package cn.wolfcode.transfer.service.impl;

import cn.wolfcode.transfer.domain.Department;
import cn.wolfcode.transfer.domain.Employee;
import cn.wolfcode.transfer.domain.Role;
import cn.wolfcode.transfer.exception.MyLoginException;
import cn.wolfcode.transfer.mapper.EmployeeMapper;
import cn.wolfcode.transfer.mapper.EmployeeRoleMapper;
import cn.wolfcode.transfer.mapper.RoleMapper;
import cn.wolfcode.transfer.qo.QueryObject;
import cn.wolfcode.transfer.service.IEmployeeService;
import cn.wolfcode.transfer.util.StringMyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public int insert(Employee employee,Long[] ids) {
        int insert = employeeMapper.insert(employee);
        for(Long id:ids) {
            employeeRoleMapper.insert(employee.getId(),id );
        }
        return insert;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        int i = employeeMapper.deleteByPrimaryKey(id);
        employeeRoleMapper.delete(id);
        return i;

    }

    @Override
    public int updateByPrimaryKey(Employee employee) {
        int i = employeeMapper.updateByPrimaryKey(employee);
        employeeRoleMapper.delete(employee.getId());

        return i;
    }

    @Override
    public Employee selectByPrimaryKey(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> selectAll() {
        List<Employee> employees = employeeMapper.selectAll();
        return employees;
    }

    @Override
    public PageInfo<Employee> getPageInfo(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.totalList(qo);
        return new PageInfo<Employee>(employees);
    }

    @Override
    public List<Role> getRoles() {
        List<Role> roles = roleMapper.selectAll();
        return roles;
    }

    @Override
    public List<Role> getEmployeeRole(Long id) {
        List<Role> roles = roleMapper.selectById(id);
        return roles;
    }

    @Override
    public void insert(Employee employee) {
        int insert = employeeMapper.insert(employee);

    }

    @Override
    public void updateByPrimaryKey(Employee employee, Long[] ids) {
        updateByPrimaryKey(employee);
        employeeRoleMapper.delete(employee.getId());
        for(Long id : ids) {
            employeeRoleMapper.insert(employee.getId(), id);
        }
    }

    @Override
    public Employee login(String username, String password) {
        //如果传入的username和password不为空
        //去数据库查找
        if (!StringMyUtil.notNull(username)){
            throw new MyLoginException("用户名不能为空");
        }
        if (!StringMyUtil.notNull(password)){
            throw new MyLoginException("密码不能为空");
        }
        Employee login = employeeMapper.login(username, password);
        if (!StringMyUtil.notNull(login)){
            throw new MyLoginException("账号或者密码不正确");
        }
        if (!login.isStatus()){
            throw new MyLoginException("账号已被封禁");
        }
        return login;
    }

    @Override
    public Employee selectByUsername(String username) {
        Employee employee = employeeMapper.selectByUsername(username);

        return employee;
    }

    @Override
    public Workbook exportXls() {
        HSSFWorkbook sheets = new HSSFWorkbook();
        //创建表
        HSSFSheet sheet = sheets.createSheet("员工通讯录");
        List<Employee> employees = selectAll();
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("用户名");
        row.createCell(1).setCellValue("密码");
        row.createCell(2).setCellValue("邮箱");
        row.createCell(3).setCellValue("部门");
        for (int i = 0 ;i< employees.size();i++){
            //创建行
             row = sheet.createRow(i+1);
//            //创建单元格
//            HSSFCell cell = row.createCell(2);
//            //设置内容
//            cell.setCellValue("hello");
            //创建单元格设置内容
            row.createCell(0).setCellValue(employees.get(i).getUsername());
            row.createCell(1).setCellValue(employees.get(i).getEmail());
            row.createCell(2).setCellValue(employees.get(i).getAge());
            row.createCell(3).setCellValue(employees.get(i).getDept().getName());
        }
        return sheets;
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        //通过数据流获取工作簿
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        //获取第一个工作表
        Sheet sheetAt = workbook.getSheetAt(0);
        //获取最后一行的索引
        int lastRowNum = sheetAt.getLastRowNum();
        //遍历第二行到最后一行
        for (int i = 1 ;i<=lastRowNum ; i++) {
            Row row = sheetAt.getRow(i);
            Employee employee = new Employee();
            employee.setUsername(row.getCell(0).getStringCellValue());

            employee.setEmail(row.getCell(1).getStringCellValue());
            employee.setAge((int)row.getCell(2).getNumericCellValue());
            Long id = Long.valueOf(StringUtils.split(row.getCell(3).getStringCellValue(),"-")[0]);
            Department department = new Department();
            department.setId(id);
            employee.setDept(department);
            employee.setPassword("1");
            employeeMapper.insert(employee);
        }
    }

    @Override
    public void UpdatePassword( String newpassword, Long id) {
        employeeMapper.UpdatePassword(newpassword,id);
    }

    @Override
    public void updateByStatus(boolean status,Long id) {
        employeeMapper.updateByStatus(status,id);

    }
}
